import { _decorator, Component, instantiate, Node, Prefab, renderer, resources } from 'cc';
import { PoolObject } from '../Pool/PoolObject';
const { ccclass, property } = _decorator;


@ccclass('ResourceLoadTask')
export class ResourceLoadTask {

    public onComplete: () => void = () => {};

    public list:string[] = [];

    public add(resName: string) :ResourceLoadTask{
        this.list.push(resName);
        var temp = resources.load(resName, Prefab, (err, prefab) => {
            if (err) {
                console.error('Error loading prefab:', err);
            }
            
            if (prefab) {
                ResourceManager.prefabDict[resName] = prefab;
            }
            this.list.remove(resName);
            if(this.list.length == 0)
            {
                this.onComplete();
            }
        });
        return this
    }
}

@ccclass('ResourceManager')
export class ResourceManager{

    public static poolNodeDict:Map<string,Node> = new Map();
    public static prefabDict:Map<string,Prefab> = new Map();
    private static poolObjcDict: Map<string, Node[]> = new Map();


    public static getPoolNode(poolName:string):Node{
        return this.poolNodeDict[poolName];
    }

    public static addPoolNode(poolName:string,poolNode:Node){
        this.poolNodeDict[poolName] = poolNode;
    }

    public static load(onComplete: () => void): ResourceLoadTask {
        var task = new ResourceLoadTask();
        task.onComplete = onComplete;
        return task;
    }
    
    public static spawn(resName: string, parent: Node | null = null): PoolObject | null {
        let obj: Node | null = null;
        const list = this.poolObjcDict[resName];
        var first = false;
        if (list!=null) {
            if (list.length > 0) {
                obj = list.pop()!;
                obj.active = true;
            } else {
                obj = this.create(resName);
                first = true;
            }
        } else {
            obj = this.create(resName);
            first = true;
        }
        if(parent!=null){
            obj.setParent(parent);
        }
        var result = obj.getComponent(PoolObject);
        result.resName = resName;
        if(first)
        {
            result.init();
            result.onSpawn();
            //console.log("创建" + resName);
        }
        else
        {
            result.onSpawn();
            //console.log("对象池里有，直接拿一个" + resName);
        }
        return result
    }

    public static despawn(target:PoolObject,pool:Node|null)
    {

        const list = this.poolObjcDict[target.resName];
        if(list == null)
        {
            this.poolObjcDict[target.resName] = [];
        }
        target.node.active = false;
        //console.log("回收"+target.resName);
        target.onDespawn();
        this.poolObjcDict[target.resName].push(target.node);
        if(pool != null)
        {
            target.node.setParent(pool);
        }
    }

    private static create(resName: string): Node | null {
        var prefab = this.prefabDict[resName];
        if(prefab == null)return null;
        const go = instantiate(prefab);
        if (go) {
            go.name = resName;
        } 
        return go;
    }

}


